package example.cdx.com.aliplayerdemo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;

import com.alivc.player.VcPlayerLog;
import com.aliyun.vodplayer.media.AliyunMediaInfo;
import com.aliyun.vodplayer.media.AliyunVodPlayer;
import com.aliyun.vodplayer.media.IAliyunVodPlayer;

/**
 * 视屏播放的类
 */
public class AliyunVodPlayerView extends RelativeLayout {

    private static final String TAG = AliyunVodPlayerView.class.getSimpleName();
    //视频画面
    private SurfaceView mSurfaceView;
    //播放器
    private AliyunVodPlayer mAliyunVodPlayer;

    //媒体信息
    private AliyunMediaInfo mAliyunMediaInfo;

    //对外的各种监听事件
    private IAliyunVodPlayer.OnPreparedListener mOutPreparedListener = null;

    public AliyunVodPlayerView(Context context) {
        this(context,null);
    }

    public AliyunVodPlayerView(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public AliyunVodPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVideoView();
    }

    /**
     * 初始化View
     */
    private void initVideoView() {
        //初始化播放用的surfaceView
        //initSurfaceView();
        //初始化播放器
        //initAliVcPlayer();
    }

    /**
     * 初始化播放器显示view
     */
    private void initSurfaceView() {
        mSurfaceView = new SurfaceView(getContext().getApplicationContext());
        addSubView(mSurfaceView);

        SurfaceHolder holder = mSurfaceView.getHolder();
        //增加surfaceView的监听
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                VcPlayerLog.d(TAG, " surfaceCreated = surfaceHolder = " + surfaceHolder);
                mAliyunVodPlayer.setDisplay(surfaceHolder);
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width,
                                       int height) {
                VcPlayerLog.d(TAG,
                        " surfaceChanged surfaceHolder = " + surfaceHolder + " ,  width = " + width + " , height = "
                                + height);
                mAliyunVodPlayer.surfaceChanged();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                VcPlayerLog.d(TAG, " surfaceDestroyed = surfaceHolder = " + surfaceHolder);
            }
        });
    }

    /**
     * 初始化播放器
     */
    private void initAliVcPlayer() {
        mAliyunVodPlayer = new AliyunVodPlayer(getContext());
        mAliyunVodPlayer.enableNativeLog();//设置c端的日志可用
        mAliyunVodPlayer.setDisplay(mSurfaceView.getHolder());//
        mAliyunVodPlayer.setOnPreparedListener(mOnPreparedListener);//设置准备的监听
        mAliyunVodPlayer.setOnFirstFrameStartListener(mOnFirstFrameStartListener);//首帧播放的监听
        mAliyunVodPlayer.setOnCompletionListener(mOnCompletionListener);//设置正常播放的监听

    }

    /**
     * 播放正常完成时触发
     */
    IAliyunVodPlayer.OnCompletionListener mOnCompletionListener = new IAliyunVodPlayer.OnCompletionListener() {
        @Override
        public void onCompletion() {
            Log.e(TAG,"播放正常完成");

        }
    };

    /**
     * 第一帧显示的监听
     */
    IAliyunVodPlayer.OnFirstFrameStartListener mOnFirstFrameStartListener = new IAliyunVodPlayer.OnFirstFrameStartListener() {
        /**
         * 第一帧播放的时候监听
         */
        @Override
        public void onFirstFrameStart() {
            Log.e(TAG,"第一帧显示");

        }
    };

    /**
     * 准备的监听
     */
    IAliyunVodPlayer.OnPreparedListener mOnPreparedListener = new IAliyunVodPlayer.OnPreparedListener() {
        @Override
        public void onPrepared() {
            //准备完成触发
            if (mAliyunVodPlayer == null) {
                return;
            }

            mAliyunMediaInfo =  mAliyunVodPlayer.getMediaInfo();
            if (mAliyunMediaInfo == null) {
                return;
            }

            //准备成功之后调用start方法开始播放
            if (mOutPreparedListener != null){
                mOutPreparedListener.onPrepared();
            }

        }
    };

    /**
     * addSubView 添加子view到布局中
     *
     * @param view 子view
     */
    private void addSubView(View view) {
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(view, params);//添加到布局中
    }

    /**
     * 开始播放
     */
    public void start(){
        if (mAliyunVodPlayer == null) {
            return;
        }

        IAliyunVodPlayer.PlayerState playerState = mAliyunVodPlayer.getPlayerState();
        if (playerState == IAliyunVodPlayer.PlayerState.Paused || playerState == IAliyunVodPlayer.PlayerState.Prepared || mAliyunVodPlayer.isPlaying()) {
            mAliyunVodPlayer.start();
        }
    }
}
